import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'

//官网
import Home from '@/components/home'
import VirtualCloud from '@/components/virtualCloud'
import Basic from '@/components/basic'
import container from '@/components/container'

import Cinema from '@/components/cinema'
import Catering from '@/components/catering'
import Super from '@/components/super'
import Customer from '@/components/customerCase'
import LinkAge from '@/components/linkage'
import Special from '@/components/special'
import NewsLists from '@/components/newsLists'
import NewsDetial from '@/components/newsDetial'
import OneMachine from '@/components/oneMachine'
import Contact from '@/components/contact'
import About from '@/components/about'

//后台管理
import Server_login from '@/views/server_web/login/login'
import Server_home from '@/views/server_web/home/home'
import Server_news from '@/views/server_web/home/news'
import Server_editNews from '@/views/server_web/home/editNews'
Vue.use(Router);

const router = new Router({
  mode: 'history',
  scrollBehavior (to, from, savedPosition) {
    return { x: 0, y: 0 }
  },
  routes: [
    {
      path: '/',
      component: container,
      children: [
        {
          path: '/',
          component: Home
        },
        {
          path: 'VirtualCloud',
          component: VirtualCloud
        },
        {
          path: 'basic',
          component: Basic
        },
        {
          path: '/cinema',
          component: Cinema
        },
        {
          path: '/catering',
          component: Catering
        },
        {
          path: '/super',
          component: Super
        },
        {
          path: '/customer',
          component: Customer
        },
        {
          path: '/linkage',
          component: LinkAge
        },
        {
          path: '/special',
          component: Special
        },
        {
          path:'/newsLists',
          component:NewsLists
        },
        {
          path:'/newsLists/newsDetial/:id',
          component:NewsDetial,
          name:'NewsDetial'
        },
        {
          path:"/oneMachine",
          component:OneMachine
        },
        {
          path:"/contact",
          component:Contact
        },
        {
          path:"/about",
          component:About
        }
      ]
    },
    // 后台管理
    {
      path: '/admin',
      component: Server_home,
      meta: {
        requiresAuth: true
      },
      children: [
        {
          path: '/',
          component: Server_news,
        },
        {
          path: '/news',
          component: Server_news,
        },
        {
          path: '/editNews',
          name: 'editNews',
          component: Server_editNews,
        }
      ]
    },
    {
      path: '/login',
      component: Server_login
    }
  ]
});

//注册全局钩子用来拦截导航
router.beforeEach((to, from, next) => {
  //获取store里面的token
  let token = store.state.token;
  //判断要去的路由有没有requiresAuth
  if (to.meta.requiresAuth) {

    if (token) {
      next();
    } else {
      next({
        path: '/login',
        query: {redirect: to.fullPath}  // 将刚刚要去的路由path（却无权限）作为参数，方便登录成功后直接跳转到该路由
      });
    }

  } else {
    next();//如果无需token,那么随它去吧
  }
});

export default router
